class Login {
    constructor() {
        this.checkRight = null;
        this.usr = null;
        this.pwd = null;
        this.imgUrl = "http://www.7xiwang.com/WebService/ImageValidateCode?code="; // 验证码api
        this.init();
    }

    init() {
        // 处理图片验证码
        this.loadImg();
        // 图片点击事件函数
        $("#ConfirmImage, #changeImg").click($.proxy(this.loadImg, this));
        // cookie自动添加用户名密码
        this.autoLogin();
        // 提交事件处理
        this.handleSubmit();
    }

    handleSubmit() {
        $("#loginBtn").click($.proxy(function() {
            this.checkInput();
            if (this.checkRight) {
                $("#errorMsgOut").css("display", "none");
                this.querySQL();
            }
        }, this))
    }

    querySQL() {
        if (1) { // 
            this.usr = $("#LoginName").val();
            if ($("#PassWord").val().length < 32) { // 如果为cookie的密码，则不需要MD5转换
                this.pwd = md5($("#PassWord").val());
            } else {
                this.pwd = $("#PassWord").val();
            }
        }
        // 查询数据库
        let url = "http://localhost:3002/login";
        let data = {
            usr: this.usr,
            pwd: this.pwd
        }
        let def = $.ajax({
            type: "GET",
            url: url,
            data: data,
            dataType: "JSON"
        })
        def.then(function(res) {
            console.log(res);
            switch (res.code) {
                case 3: // 登录成功
                    let TOKENINFO = "usr=" + data.usr + "&pwd=" + data.pwd;
                    $.cookie("TOKEN", TOKENINFO, {
                        path: "/",
                        expires: 15
                    });
                    window.location.href = "http://localhost:8888/index.html";
                    break;
                case 4: // 密码错误
                case 5: // 用户名不存在
                    $("#errorMsgOut").css("display", "block");
                    $(".jinggao").html(res.data);
                    break;
            }
        })
    }

    checkInput() {
        this.checkRight = true;
        $.each($(".input_style"), $.proxy(function(index, item) {
            if ($(item).attr("id") == "vc") {
                var reg = new RegExp("^" + this.code + "$", "i");
                if (!reg.test($(item).val())) {
                    $("#errorMsgOut").css("display", "block");
                    $(".jinggao").html("验证码未输入或长度不对！");
                    this.checkRight = false;
                    return false;
                }
            } else {
                if ($(item).val().length === 0) {
                    $("#errorMsgOut").css("display", "block");
                    $(".jinggao").html($(item).attr("placeholder"));
                    this.checkRight = false;
                    return false;
                }
            }
        }, this))
    }

    autoLogin() {
        if ($.cookie("TOKEN") && $.cookie("TOKEN") !== "null") {
            let cookie = $.cookie("TOKEN");
            this.usr = cookie.split("&")[0].split("=")[1];
            this.pwd = cookie.split("&")[1].split("=")[1];
            $("#LoginName").val(this.usr);
            $("#PassWord").val(this.pwd);
        }
    }

    loadImg() {
        this.code = this.GetRandomCode(4); // 四位验证码
        $("#ConfirmImage").attr("src", this.imgUrl + this.code);
    }

    //n表示生成随机码的位数 
    GetRandomCode(n) { 
        let chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']; 
        let res = ""; 
        for (let i = 0; i < n; i++) {  
            let id = Math.ceil(Math.random() * 35);  
            res += chars[id]; 
        } 
        return res;
    }
}

new Login();
// 获取cookie => $.cookie("cookie")